package com.vdimitr.happycube.puzzle.matrix.bycolor;

import com.vdimitr.happycube.puzzle.matrix.MatrixUtils;
import com.vdimitr.happycube.puzzle.piece.PuzzlePieceConstants;
import com.vdimitr.happycube.puzzle.utils.RangeChecker;

/**
 * This class holds the matrix representations of the puzzle pieces. Each matrix
 * should be 5x5 filled with 0s and 1s. 0 means there is a square part missing
 * from the puzzle piece and 1 means there is not.
 * 
 * Please have a look at the src/main/recources/cubes.png
 * 
 * @author vasileios.dimitriadis
 * 
 */
public abstract class PuzzlePieceMatrices {

	/**
	 * The minimum index of the puzzle piece number for which to request a
	 * matrix representation
	 */
	public static final int INCLUSIVE_LOWER_BOUND = 1;

	/**
	 * The maximum index of the puzzle piece number for which to request a
	 * matrix representation
	 */
	public static final int INCLUSIVE_UPPER_BOUND = PuzzlePieceConstants.NUMBER_OF_PUZZLEPIECES;

	/**
	 * An array holding the 5x5 matrices representing puzzle pieces
	 */
	protected int[][][] puzzlePieces;

	/**
	 * Returns a copy of the matrix representation of a puzzle piece
	 * 
	 * @param puzzlePieceNumber
	 *            must be between 1 and 6 (inclusive)
	 * @return the matrix representing the puzzle piece with the specific number
	 */
	public int[][] getPuzzlePieceMatrix(int puzzlePieceNumber) {
		puzzlePieceNumberRangeCheck(puzzlePieceNumber);
		return MatrixUtils.deepCopy(puzzlePieces[puzzlePieceNumber - 1]);
	}

	/**
	 * Checks the given puzzle piece number falls within range. If not an
	 * IllegalArgumentException is thrown
	 * 
	 * @param puzzlePieceNumber
	 *            must be between 1 and 6 (inclusive)
	 * @throws IllegalArgumentException
	 *             in case the puzzlePieceNumber is not within range
	 */
	protected void puzzlePieceNumberRangeCheck(int puzzlePieceNumber) {
		if (RangeChecker.isOutOfRange(INCLUSIVE_LOWER_BOUND,
				INCLUSIVE_UPPER_BOUND, puzzlePieceNumber)) {
			throw new IllegalArgumentException(
					"Puzzle piece number must be between "
							+ INCLUSIVE_LOWER_BOUND + " and  "
							+ INCLUSIVE_UPPER_BOUND);
		}
	}

}
